
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;

import com.vmware.vim25.InvalidProperty;
import com.vmware.vim25.PerfEntityMetric;
import com.vmware.vim25.PerfEntityMetricBase;
import com.vmware.vim25.PerfMetricId;
import com.vmware.vim25.PerfMetricIntSeries;
import com.vmware.vim25.PerfMetricSeries;
import com.vmware.vim25.PerfProviderSummary;
import com.vmware.vim25.PerfQuerySpec;
import com.vmware.vim25.PerfSampleInfo;
import com.vmware.vim25.RuntimeFault;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.PerformanceManager;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class DiplayHostCounter {

	static final String SERVER_NAME = "10.16.2.59";
	static final String USER_NAME = "root";
	static final String PASSWORD = "vmware";
	private static final String HOSTNAME = "10.16.2.32";
	private static final int SELECTED_COUNTER_ID = 1; // Active (mem) in KB (absolute)

	public static void main(String[] args) {
		String url = "https://" + SERVER_NAME + "/sdk";
		try {
			ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
			
//			 VirtualMachine targetVm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine","ESB[172.20.41.218]");;
			VirtualMachine host = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine","ESB[172.20.41.218]");
			PerformanceManager perfMgr = si.getPerformanceManager();
			System.out.println("Counter ID = " + SELECTED_COUNTER_ID);

			PerfProviderSummary summary = perfMgr.queryPerfProviderSummary(host);
			int perfInterval = summary.getRefreshRate();
			System.out.println("Refresh rate = " + perfInterval);

			PerfMetricId[] queryAvailablePerfMetric = perfMgr.queryAvailablePerfMetric(host, null, null, perfInterval);
			ArrayList<PerfMetricId> list = new ArrayList<PerfMetricId>();
			for (int i2 = 0; i2 < queryAvailablePerfMetric.length; i2++) {
				PerfMetricId perfMetricId = queryAvailablePerfMetric[i2];
				if (SELECTED_COUNTER_ID == perfMetricId.getCounterId()) {
					list.add(perfMetricId);
				}
			}
			PerfMetricId[] pmis = list.toArray(new PerfMetricId[list.size()]);

			PerfQuerySpec qSpec = new PerfQuerySpec();
			qSpec.setEntity(host.getMOR());
			qSpec.setMetricId(pmis);
			qSpec.intervalId = perfInterval;
			qSpec.setFormat("normal");

			PerfEntityMetricBase[] pembs = perfMgr.queryPerf(new PerfQuerySpec[] { qSpec });
			for (int i = 0; pembs != null && i < pembs.length; i++) {
				PerfEntityMetricBase val = pembs[i];
				PerfEntityMetric pem = (PerfEntityMetric) val;
				PerfMetricSeries[] vals = pem.getValue();
				PerfSampleInfo[] infos = pem.getSampleInfo();

				for (int j = 0; vals != null && j < vals.length; ++j) {
					PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[j];
					long[] longs = val1.getValue();
					for (int k = 0; k < longs.length; k++) {
						System.out.println(infos[k].getTimestamp().getTime() + " : " + longs[k]);
					}
					System.out.println();
				}
			}
			si.getServerConnection().logout();
		} catch (InvalidProperty e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RuntimeFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}